package com.qf.fmall2203.shopcart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.fmall2203.product.entity.Product;
import com.qf.fmall2203.product.entity.ProductImg;
import com.qf.fmall2203.product.entity.ProductSku;
import com.qf.fmall2203.product.service.IProductService;
import com.qf.fmall2203.product.service.IProductSkuService;
import com.qf.fmall2203.shopcart.entity.ShoppingCart;
import com.qf.fmall2203.shopcart.mapper.ShoppingCartMapper;
import com.qf.fmall2203.shopcart.service.IShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 购物车  服务实现类
 * </p>
 *
 * @author dongjl
 * @since 2023-02-16
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {

    @Autowired
    IProductService productService;

    @Autowired
    IProductSkuService productSkuService;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ShoppingCart> listShopcart(Integer userId) {

        //1. 根据userid查询 购物车信息
        final List<ShoppingCart> cartList = list(new QueryWrapper<ShoppingCart>().eq("user_id", userId));
        //2. 查询商品名称
        // List<ShoppingCart> --->  List<productId>
        final List<String> productIdList = cartList.stream().map(ShoppingCart::getProductId).collect(Collectors.toList());
        final List<Product> productList = productService.list(new QueryWrapper<Product>().in("product_id", productIdList));
        //3. 查询商品图片url
        productService.populateImg(productList);
        //4. 查询商品套餐信息
        final List<ProductSku> skuList = productSkuService.list(new QueryWrapper<ProductSku>().in("product_id", productIdList));

        //5. 组织数据
        populateCart(cartList,productList,skuList);

        return cartList;
    }

    @Override
    public List<ShoppingCart> listbycids(Integer[] cids) {

        //1. 根据userid查询 购物车信息
//        final List<ShoppingCart> cartList = list(new QueryWrapper<ShoppingCart>().eq("user_id", userId));

        final List<ShoppingCart> cartList = list(new QueryWrapper<ShoppingCart>().in("cart_id",cids));

        //2. 查询商品名称
        // List<ShoppingCart> --->  List<productId>
        final List<String> productIdList = cartList.stream().map(ShoppingCart::getProductId).collect(Collectors.toList());
        final List<Product> productList = productService.list(new QueryWrapper<Product>().in("product_id", productIdList));
        //3. 查询商品图片url
        productService.populateImg(productList);
        //4. 查询商品套餐信息
        final List<ProductSku> skuList = productSkuService.list(new QueryWrapper<ProductSku>().in("product_id", productIdList));

        //5. 组织数据
        populateCart(cartList,productList,skuList);

        return cartList;
    }

    /**
     * 需要补充如下字段的信息
     *             "productName": "咪咪小虾条",
     *
     *             "productImg": "mmxt_1.png",
     *
     *             "skuName": "咪咪虾条 单包 50g",
     *             "originalPrice": 2,
     *             "sellPrice": 1,
     *             "stock": 277
     * @param cartList
     * @param productList
     * @param skuList
     */
    private void populateCart(List<ShoppingCart> cartList, List<Product> productList, List<ProductSku> skuList) {

        // List<Product> productList ---->  Map<productId,Product>
        final Map<String, Product> productMap = productList.stream().collect(Collectors.toMap(Product::getProductId, p -> p));

        // List<ProductSku> ----> Map<skuId,ProductSKu>
        final Map<String, ProductSku> skuMap = skuList.stream().collect(Collectors.toMap(ProductSku::getSkuId, s -> s));


        for (ShoppingCart cart : cartList) {
            final String productId = cart.getProductId();
            // 从map中查出对应的商品对象
            final Product product = productMap.get(productId);
            cart.setProductName(product.getProductName());

            // 获取商品图片url
            final List<ProductImg> imgs = product.getImgs();
            // 选取主图片的url
            for (ProductImg img : imgs) {
                if (img.getIsMain().equals(1)) {
                    cart.setProductImg(img.getUrl());
                    break;
                }
            }

            // 填充套餐信息
            final String skuId = cart.getSkuId();
            final ProductSku productSku = skuMap.get(skuId);
            cart.setSkuName(productSku.getSkuName());
            cart.setOriginalPrice(productSku.getOriginalPrice());
            cart.setSellPrice(productSku.getSellPrice());
            cart.setStock(productSku.getStock());


        }
    }
}
